<?php

class option {

    function option() {
    }
    
    //Check whether the $key is exist in database or not
    function isExistKey($db, $key)
    {
    	$sql = sprintf("Select count(option_key) from %s where option_key = '$key'",
    					TBL_OPTION);
    	
    	return ($db->get_var($sql) > 0);
    }
    
    //add a value of option table
    function add($db, $key, $value)
    {
    	$sql = sprintf("insert into %s (option_key, option_value) " .
    					"values('$key', '$value')",
    					TBL_OPTION);
    	
    	return $db->query($sql);
    }
    
     //update a value of option table
    function update($db, $key, $value)
    {
    	$sql = sprintf("update %s set " .
    					"option_value = '$value' " .
    					"where option_key = '$key'",
    					TBL_OPTION);
    	
    	return $db->query($sql);
    }
    
    //Save or update a value of option table
    function saveOption($db, $key, $value)
    {
    	if(option::isExistKey($db, $key))
    	{
    		return option::update($db, $key, $value);
    	}
    	else
    	{
    		return option::add($db, $key, $value);
    	}
    }
    
    function loadAllOption($db)
    {
    	$sql = sprintf("select * from %s",
    					TBL_OPTION);
    	
    	return $db->get_results($sql);
    }
}
?>